package com.cpit.platform.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dto.SubSystem;
import com.cpit.platform.service.SubSystemService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/SubSystem")
public class SubSystemController {

	@Autowired
	private SubSystemService subsystemService;
	
	@ApiOperation(value="查询子系统", notes="通过条件查询子系统" + 
	"支持分页查询，支持模糊查询。")
	@ResponseBody
	@PreAuthorize("hasAuthority('querySubSystem')")
	@RequestMapping(value="/querySubSystem", method = RequestMethod.GET)  
	public List<SubSystem> querySubSystem(
			@ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = true, defaultValue = "1") int page, 
			@ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = true, defaultValue = "20") int size, 
			@ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order, 
			@ApiParam(value = "排序字段，与dto中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy, 
			@ApiParam(value = "模糊搜索的字符串，根据子系统的名称字段进行模糊匹配。") @RequestParam(value = "sysName", required = false, defaultValue = "") String sysName)  {
		return subsystemService.findByCondition(page, size, order, orderBy, sysName);
	}  
	@ApiOperation(value="查询子系统",notes="通过id查找子系统")
	@ResponseBody
	@PreAuthorize("hasAuthority('querySubSystem')")
	@RequestMapping(value="/getSubSystem",method = RequestMethod.GET)
	public SubSystem getSubSystem(
			@ApiParam(value = "搜索id，子系统的id字段进行匹配。") @RequestParam(value = "sysId", required = false, defaultValue = "") Integer sysId)  {
		return subsystemService.findByID(sysId);
	}
	
	@ApiOperation(value="查询子系统列表", notes="通过条件查询系统中的子系统信息" + 
			"支持分页查询，支持模糊查询。")
		    @ApiImplicitParams({
		            @ApiImplicitParam(name = "cond", value = "JqGrid的条件对象，由JqGrid根据js参数自动封装。", required = true, dataType = "TableDataQueryCondition")
		    })
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@PreAuthorize("hasAuthority('querySubSystem')")
	@RequestMapping(value="/querySubSystemJqg", method = RequestMethod.GET)  
	public TableJsonData querySubSystemJqg(TableDataQueryCondition cond) {
		TableJsonData tjd = subsystemService.findByConditionJqg(cond);
		return tjd;
	}  
	
	@ApiOperation(value="创建子系统", notes="用户手工指定子系统并创建，" + 
			"需要手动输入系统的详细信息，包括名称、描述，以生产厂商、系统类型、登录类型。")
		     @ApiImplicitParam(name = "data", value = "子系统描述对象。", required = true, dataType = "SubSystem")
	@ResponseBody
	@PreAuthorize("hasAuthority('createSubSystem')") 
	@RequestMapping(value="/createSubSystem", method = RequestMethod.POST)  
	public Result<SubSystem> createSubSystem(@RequestBody SubSystem data) {
		try{
			subsystemService.create(data);
			return new Result<SubSystem>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			return new Result<SubSystem>(Result.CODE_FAILED, "", data);
		}
	}  
	
	@ApiOperation(value="修改子系统", notes="用户手工指定子系统并修改，" + 
			"需要手动输入子系统的详细信息，包括名称、描述，以生产厂商、系统类型、登录类型。")
		     @ApiImplicitParam(name = "data", value = "子系统描述对象。", required = true, dataType = "SubSystem")
	@ResponseBody
	@PreAuthorize("hasAuthority('modifySubSystem')")
	@RequestMapping(value="/modifySubSystem", method = RequestMethod.POST)  
	public Result<SubSystem> modifySubSystem(@RequestBody SubSystem data) {
		try{
			subsystemService.create(data);
			return new Result<SubSystem>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			return new Result<SubSystem>(Result.CODE_FAILED, "", data);
		}
	}
	
	@ApiOperation(value="删除子系统", notes="在系统中删除已经存在的子系统，")
		     @ApiImplicitParam(name = "data", value = "子系统描述对象。", required = true, dataType = "SubSystem")
	@ResponseBody
	@PreAuthorize("hasAuthority('deleteSubSystem')")
	@RequestMapping(value="/deleteSubSystem", method = RequestMethod.DELETE)  
	public Result<SubSystem> deleteSubSystem(@RequestBody SubSystem data) {
		try{
			subsystemService.delete(data);
			return new Result<SubSystem>(Result.CODE_SUCCESS, "", data);
		} catch(Exception e) {
			return new Result<SubSystem>(Result.CODE_FAILED, "", data);
		}
	}
	
}
